# -*- coding: utf-8 -*-
from Products.CMFCore.utils import getToolByName
from zope.app.component.hooks import getSite

from Products.CMFPlone.utils import safe_unicode
from Products.CMFPlone import PloneMessageFactory as _
from Products.CMFPlone.utils import transaction_note
from zope.component import getMultiAdapter
import logging
from babble.client.utils import getConnection
from babble.client.interfaces import IBabbleClientLayer

log = logging.getLogger(__name__)

def setupGroups(portal):
    acl_users = getToolByName(portal, 'acl_users')
    mt = getToolByName(portal, 'portal_membership')
    users = mt.listMemberIds()

    for user in users:
        member = mt.getMemberById(user)
        registraActualesUsersEnChat(portal,member)


    if not acl_users.searchGroups(name='ChatResponsables'):
        gtool = getToolByName(portal, 'portal_groups')
        gtool.addGroup('ChatResponsables', roles=['StaffMember'])


def importVarious(context):
    """Inicializa el seteo
    """
    portal = getSite()
    """
    try:
        mountStructure(portal)
    except :
        print "no se puede crear la estructura"
    """


    #setupGroups(portal)


def registraActualesUsersEnChat(portal,user):
    """registra usuarios actuales en el chat.
    A medida que se dan de alta, automaticamente se crean
    en el servidor"""

    site = portal
    userId= user.getId()

    # Register the user for the chatservice
    view = getMultiAdapter((site, site.REQUEST), name='babblechat')
    view.initialize(userId)

    #s = getConnection(site)
    #registrado=s.register(userId,"passchat")

